package com.hao.hospital.Interceptor;

import org.apache.commons.lang.StringUtils;

import com.hao.hospital.core.param.GlobalMap;
import com.hao.hospital.model.User;
import com.hao.hospital.util.WeiXinUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;

public class WeiXinUserInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		String openid = inv.getController().getSessionAttr("openid");
		System.out.println(openid);
		if (StringUtils.isNotEmpty(openid)) {
			String name = SqlBuilder.from(User.table).where("openid=?")
					.find(inv.getController().getSessionAttr("openid").toString()).get("user_name");
			if (StringUtils.isNotEmpty(name)) {
				inv.invoke();
			} else {
				//老用户没填写个人信息的
				redirect(inv.getController());
				System.out.println("执行老用户没填写个人信息的");
			}
		} else {
			//新老用户没授权(老用户既没授权又没填写个人信息)
			redirect(inv.getController());
			System.out.println("执行新老用户没授权");
		}
	}
	/**
	 * 跳转
	 * @param c
	 */
	public void redirect(Controller c){
		String url = c.getRequest().getRequestURI();
		c.setSessionAttr("url", url);
		c.redirect(WeiXinUtil.me().initCodeURL());
		System.out.println(WeiXinUtil.me().initCodeURL());
		
	}

}
